package by.grsu.hdm.model;

import by.grsu.hdm.exception.IllegalInternalStateException;

public abstract class HomeDevice implements IHomeDevice {

	private String id;
	private boolean turnstate;
	
	protected HomeDevice() {
	}
	
	public HomeDevice(String id) {
		this.id = id;
	}

	@Override
	public HomeDeviceStatus turnOn() throws IllegalInternalStateException {
		if(turnstate) throw new IllegalInternalStateException("ERROR:"+this.id+"is already turned on");
		this.turnstate=true;
		this.afterTurningOn();
		return new HomeDeviceStatus(1, "The device " + getDeviceId() + " is turned on");
	}

	@Override
	public HomeDeviceStatus turnOff() throws IllegalInternalStateException {
		if(!turnstate) throw new IllegalInternalStateException("ERROR:"+this.id+"is already turned off");
		this.turnstate=false;
		this.beforeTurningOff();
		return new HomeDeviceStatus(2, "The device " + getDeviceId() + " is turned off");
	}
	
	protected void afterTurningOn() { System.out.println(this.id+" is ON"); }
	
	protected void beforeTurningOff() { System.out.println(this.id + " is OFF"); }
	
	public String getDeviceId() {
		return this.id;
	}
	
	protected void defaultConfiguration() { }
}
